package com.clementheliou.cheliou.util.time;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.LocalDateTime;

/**
 * Utilities about time allowing to manage it, particularly for test purpose. This class is mainly
 * base on the Joda Time API utilities.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @since 1.0
 */
public abstract class TimeUtils {

	/**
	 * Returns the present time.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the present time as a {@link LocalDateTime}.
	 * @since 1.0
	 */
	public static LocalDateTime now() {
		return LocalDateTime.now();
	}

	/**
	 * Fixes the time with the present one plus 5 minutes. It shoudl be mostly used after a call to
	 * the {@link #setFixedTime(LocalDateTime)} method. Until you call the {@link #resetTime()}
	 * method, this time won't change and will be returned whenever you call {@link #now()} or
	 * {@link LocalDateTime}/ {@link DateTime} constructors.
	 * <p>
	 * <b>Note: </b> This method must only be used in <tt>test</tt> packages.
	 * </p>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	public static void fiveMinutesLater() {
		setFixedTime(now().plusMinutes(5));
	}

	/**
	 * Fixes the time with the given one. Until you call the {@link #resetTime()} method, this time
	 * won't change and will be returned whenever you call {@link #now()} or {@link LocalDateTime}/
	 * {@link DateTime} constructors.
	 * <p>
	 * <b>Note: </b> This method must only be used in <tt>test</tt> packages.
	 * </p>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param timeToSet
	 * @since 1.0
	 */
	public static void setFixedTime(final LocalDateTime timeToSet) {
		DateTimeUtils.setCurrentMillisFixed(timeToSet.toDateTime().getMillis());
	}

	/**
	 * Reset the time with the system one. Should be called after all the case that set a fixed
	 * time.
	 * <p>
	 * <b>Note: </b> This method must only be used in <tt>test</tt> packages.
	 * </p>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	public static void resetTime() {
		DateTimeUtils.setCurrentMillisSystem();
	}
}
